﻿<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label w-auto">搜索：</label>
                        <div class="layui-input-inline mr0">
                            <input name="keyword" class="layui-input" type="text" placeholder="输入关键字"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="formSubSearchRole" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                        <button id="btnAddRole" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="tableRole" lay-filter="tableRole"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="tableBarRole">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
	<a class="layui-btn layui-btn-xs" lay-event="authForMenu">系统权限分配</a>
	<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="authForSceneMenu">场景权限分配</a>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="modelRole">
    <form id="modelRoleForm" lay-filter="modelRoleForm" class="layui-form model-form">
        <input name="ID" type="hidden"/>
        <div class="layui-form-item">
            <label class="layui-form-label">角色名</label>
            <div class="layui-input-block">
                <input name="NAME" placeholder="请输入角色名" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">标识</label>
            <div class="layui-input-block">
                <input name="CODE" placeholder="请输入标识" type="text" class="layui-input" maxlength="20"
                       lay-verType="tips" lay-verify="required" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="REMARK" placeholder="请输入内容" class="layui-textarea" maxlength="200"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">序号</label>
            <div class="layui-input-block">
                <input name="SORT" placeholder="请输入序号" type="number" class="layui-input" min="0" max="1000"
                       lay-verType="tips" lay-verify="required|number" required/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="modelSubmitRole" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="modelRoleData">
    <div class="layui-card">
        <div class="layui-card-body">
            <table class="layui-table" id="tableRoleData" lay-filter="tableRoleData"></table>
        </div>
    </div>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="tableBarRoleData">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="authData">授权</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancelData">取消</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="modelmodelRoleDataForm"> 
    <div class="layui-card">
        <div class="layui-card-body">
		    <form id="modelRoleDataForm" lay-filter="modelRoleDataForm" class="layui-form model-form">
		        <input name="ID" type="hidden"/>
				  <div class="layui-form-item">
				    <label class="layui-form-label">范围</label>
				    <div class="layui-input-block">
				      <select name="interest" lay-filter="aihao">
				        <option value=""></option>
				        <option value="0">当前部门</option>
				        <option value="1">当前部门以及子部门</option>
				      </select>
				    </div>
				  </div>				
		        <div class="layui-form-item">
		            <label class="layui-form-label">权限</label>
		            <div class="layui-input-block">
						<input id="parentId" type="text" name="PARENTID"   lay-filter="parentId" autocomplete="off" placeholder="请选择" value="" class="layui-input">
		            </div>
		        </div>
		        <div class="layui-form-item text-right">
		            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
		            <button class="layui-btn" lay-filter="modelSubmitRole" lay-submit>保存</button>
		        </div>
		    </form>
        </div>
    </div>
</script>

<script type="text/html" id="modelRoleSceneAuthScript">
   <div class="layui-card">
       <div class="layui-card-body table-tool-mini">
           <div class="layui-form toolbar" id="tbToolBar">
               <div class="layui-form-item">
                   <div class="layui-inline" style="float: left;">
					<div class="layui-inline" style="padding-right: 0px;margin-right: 0px;">
						<label class="layui-form-label w-auto">场景</label>
						<div class="layui-input-inline mr0">
					      <select name="sceneIdPage" lay-filter="sceneIdPage">
					        <option value=""></option>
					        #for(scene : scenes)
					        	 <option value="#(scene.id)">#(scene.title)</option>
					        #end
					      </select>
						</div>
					</div> 
				</div>
			</div>
			<ul id="treeSceneMenuAuth" class="ztree" style="height: 500px;"></ul>
		</div>
	  </div>
	</div>
</script>

<!-- js部分 -->
<script>
layui.extend({
    treeSelect: '/treeSelect/treeSelect'
}).use(['layer', 'form', 'table', 'util', 'admin', 'zTree','treetable', 'config','treeSelect'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var config = layui.config;
        var treetable = layui.treetable;
        var treeSelect=layui.treeSelect;
        var config = layui.config;
        // 渲染表格
        var insTb = table.render({
            elem: '#tableRole',
            url: '#(ctx)/sys/role/listData',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'NAME', sort: true, title: '角色名'},
                {field: 'CODE', sort: true, title: '标识'},
                {field: 'REMARK', sort: true, title: '备注'},
                {align: 'center', toolbar: '#tableBarRole', title: '操作', minWidth: 150}
            ]]
        });

        // 添加
        $('#btnAddRole').click(function () {
            showEditModel();
        });

        // 搜索
        form.on('submit(formSubSearchRole)', function (data) {
            insTb.reload({where: data.field}, 'data');
        });

        // 工具条点击事件
        table.on('tool(tableRole)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'del') { // 删除
                doDel(obj);
            } else if (layEvent === 'authForMenu') {  // 权限管理
                showPermModel(data.ID);
            }
            else if (layEvent === 'authForSceneMenu') {  // 权限管理
            	showScenePermModel(data.ID);
            }
            
        });

        // 删除
        function doDel(obj) {
            layer.confirm('确定要删除“' + obj.data.NAME + '”角色吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('sys/role/delete', {
                    id: obj.data.ID
                }, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.message, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.message, {icon: 2});
                    }
                }, 'get');
            });
        }

        // 显示编辑弹窗
        function showEditModel(mRole) {
            admin.open({
                type: 1,
                title: (mRole ? '修改' : '添加') + '角色',
                content: $('#modelRole').html(),
                success: function (layero, dIndex) {
                    var url = 'sys/role/save';
                    form.val('modelRoleForm', mRole);  // 回显数据
                    // 表单提交事件
                    form.on('submit(modelSubmitRole)', function (data) {
                        layer.load(2);
                        var formData={}; 
                        for(var key in data.field){
                        	formData['sysRole.'+key]=data.field[key];
                        }
                        admin.req(url, formData, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.close(dIndex);
                                layer.msg(res.message, {icon: 1});
                                insTb.reload({}, 'data');
                            } else {
                                layer.msg(res.message, {icon: 2});
                            }
                        }, 'get');
                        return false;
                    });
                }
            });
        }

        // 系统权限管理
        function showPermModel(roleId) {
            admin.open({
                title: '角色系统权限分配',
                btn: ['保存', '取消'],
                offset: 't',
                content: '<ul id="treeAuth" class="ztree"></ul>',
                success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css({'max-height': '500px', 'overflow': 'auto'});
                    layer.load(2);
                    var setting = {check: {enable: true}, data: {simpleData: {enable: true}}};
                    admin.req('sys/role/authTree', {
                        roleId: roleId
                    }, function (res) {
                        $.fn.zTree.init($('#treeAuth'), setting, res);
                        layer.closeAll('loading');
                    }, 'get');
                },
                yes: function (index) {
                    layer.load(2);
                    var treeObj = $.fn.zTree.getZTreeObj('treeAuth');
                    var nodes = treeObj.getCheckedNodes(true);
                    var ids = new Array();
                    for (var i = 0; i < nodes.length; i++) {
                        ids[i] = nodes[i].id;
                    }
                    
                    admin.req('sys/role/saveAuth', {
                        roleId: roleId,
                        authIds: JSON.stringify(ids)
                    }, function (res) {
                        layer.closeAll('loading');
                        if (200 == res.code) {
                            layer.msg(res.message, {icon: 1});
                            layer.close(index);
                        } else {
                            layer.msg(res.message, {icon: 2});
                        }
                    }, 'get');
                }
            });
        }
        
        var targetId;
        function showScenePermModel(roleId) {
            admin.open({
                title: '角色场景权限分配',
                btn: ['保存', '取消'],
                offset: 't',
                content: $('#modelRoleSceneAuthScript').html(),
                success: function (layero, index) {
                    //$(layero).children('.layui-layer-content').css({'max-height': '500px', 'overflow': 'auto'});
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    form.render();
                    var setting = {check: {enable: true}, data: {simpleData: {enable: true}}};
	              	form.on('select(sceneIdPage)', function (data) {
	              		 layer.load(2);
	              		 targetId=data.value;
	                     admin.req('sys/role/authSceneTree', {
	                         roleId: roleId,
	                         sceneId: data.value
	                     }, function (res) {
	                         $.fn.zTree.init($('#treeSceneMenuAuth'), setting, res);
	                         layer.closeAll('loading');
	                     }, 'get');
	                });
                },
                yes: function (index) {
                     layer.load(2);
                    var treeObj = $.fn.zTree.getZTreeObj('treeSceneMenuAuth');
                    var nodes = treeObj.getCheckedNodes(true);
                    var ids = new Array();
                    for (var i = 0; i < nodes.length; i++) {
                        ids[i] = nodes[i].id;
                    }
                    
                    admin.req('sys/role/saveAuth', {
                        roleId: roleId,
                        authIds: JSON.stringify(ids),
                        targetId:targetId
                    }, function (res) {
                        layer.closeAll('loading');
                        if (200 == res.code) {
                            layer.msg(res.message, {icon: 1});
                            layer.close(index);
                        } else {
                            layer.msg(res.message, {icon: 2});
                        }
                    }, 'get'); 
                }
            });
        }
        
        
    });
</script>